package com.jianshuge.app;

import com.jianshuge.app.R;
import com.jianshuge.app.AppConfig;
import com.jianshuge.app.bean.AccessInfo;
import com.jianshuge.app.common.SinaWeiboHelper;
import com.jianshuge.app.common.DoubanHelper;
import com.jianshuge.app.common.QCHelper;
import com.jianshuge.app.common.StringUtils;


import android.graphics.drawable.AnimationDrawable;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewSwitcher;

/**
 * 应用程序启动类：显示欢迎界面并跳转到主界面
 */
public class AppStart extends Activity {
	
	private Button mLoginSina;
	private Button mLoginDouban;
	private Button mLoginQQ;
	private Button mLoginNormal;
	
	private ViewSwitcher mViewSwitcher;
	private View loginLoading;
	private AnimationDrawable loadingAnimation;

	private TextView mNetCheck;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final View view = View.inflate(this, R.layout.start, null);
		setContentView(view);

		mViewSwitcher = (ViewSwitcher)findViewById(R.id.start_view_switcher);  
		loginLoading = (View)findViewById(R.id.login_loading);
		
		mNetCheck 	 = (TextView)view.findViewById(R.id.netCheckTxt); 
		mLoginDouban = (Button)view.findViewById(R.id.btn_login_douban);
		mLoginQQ 	 = (Button)view.findViewById(R.id.btn_login_qq);
		mLoginSina 	 = (Button)view.findViewById(R.id.btn_login_sina); 
		mLoginNormal 	 = (Button)view.findViewById(R.id.btn_login_normal); 
		
		mLoginDouban.setOnClickListener(loginClickListener);
		mLoginQQ.setOnClickListener(loginClickListener);
		mLoginSina.setOnClickListener(loginClickListener);
		mLoginNormal.setOnClickListener(loginClickListener);

		//渐变展示启动屏
		AlphaAnimation aa = new AlphaAnimation(0.3f,1.0f);
		aa.setDuration(2000);
		view.startAnimation(aa);
		aa.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation arg0) {
			//	redirectTo();
			}
			@Override
			public void onAnimationRepeat(Animation animation) {}
			@Override
			public void onAnimationStart(Animation animation) {}
			
		});
		
		//兼容低版本cookie（1.5版本以下，包括1.5.0,1.5.1）
		AppContext appContext = (AppContext)getApplication();
		String cookie = appContext.getProperty("cookie");
		if(StringUtils.isEmpty(cookie)) {
			String cookie_name = appContext.getProperty("cookie_name");
			String cookie_value = appContext.getProperty("cookie_value");
			if(!StringUtils.isEmpty(cookie_name) && !StringUtils.isEmpty(cookie_value)) {
				cookie = cookie_name + "=" + cookie_value;
				appContext.setProperty("cookie", cookie);
				appContext.removeProperty("cookie_domain","cookie_name","cookie_value","cookie_version","cookie_path");
			}
		}
		
		//网络连接判断
		if (!appContext.isNetworkConnected()) {
		//	UIHelper.ToastMessage(this, R.string.network_not_connected);
			mNetCheck.setVisibility(View.VISIBLE);
			mNetCheck.setText(R.string.network_not_connected);
			
		}
    }
    
    private View.OnClickListener loginClickListener = new View.OnClickListener() {		
		public void onClick(View v) {
			
			if( v == mLoginDouban ) {
	
				AppConfig cfgHelper = AppConfig.getAppConfig(v.getContext());
				final AccessInfo access = cfgHelper.getAccessInfo();
				
				// 分享的内容
				final String shareMessage = "豆瓣登陆" + " http://www.jianshuge.com";
				
				// 初始化
				if (DoubanHelper.isDoubanNull()) {
					DoubanHelper.initDouban();
				}
				
				// 判断之前是否登陆过
				if (access != null && access.getLoginType().equals("douban") ) {
					
			        loadingAnimation = (AnimationDrawable)loginLoading.getBackground();
			        loadingAnimation.start();
			        mViewSwitcher.showNext();
					DoubanHelper.login(AppStart.this, access.getAccessToken(), access.getLoginType());
					
				} else {

			        loadingAnimation = (AnimationDrawable)loginLoading.getBackground();
			        loadingAnimation.start();
			        mViewSwitcher.showNext();
					DoubanHelper.authorize(AppStart.this, shareMessage);
				}
			}
			
			if( v == mLoginQQ ) {
				
				AppConfig cfgHelper = AppConfig.getAppConfig(v.getContext());
				final AccessInfo access = cfgHelper.getAccessInfo();
				
				// 分享的内容
				final String shareMessage = "QQ登录" + " http://www.jianshuge.com";
				
				// 初始化QC
				if (QCHelper.isQCNull()) {
					QCHelper.initQC();
				}
				
				// 判断之前是否登陆过
				if (access != null && access.getLoginType().equals("qq") ) {

			        loadingAnimation = (AnimationDrawable)loginLoading.getBackground();
			        loadingAnimation.start();
			        mViewSwitcher.showNext();
					QCHelper.login(AppStart.this, access.getAccessToken(), access.getLoginType());
										
				} else {
					
			        loadingAnimation = (AnimationDrawable)loginLoading.getBackground();
			        loadingAnimation.start();
			        mViewSwitcher.showNext();
					QCHelper.authorize(AppStart.this, shareMessage);
				
				}
			}
			
			if( v == mLoginSina) {
				
				AppConfig cfgHelper = AppConfig.getAppConfig(v.getContext());
				final AccessInfo access = cfgHelper.getAccessInfo();
				
				// 分享的内容
				final String shareMessage = "微博登陆" + " http://www.jianshuge.com";
				// 初始化微博
				if (SinaWeiboHelper.isWeiboNull()) {
					SinaWeiboHelper.initWeibo();
				}

				// 判断之前是否登陆过
				if (access != null && access.getLoginType().equals("weibo") ) {
					
			        loadingAnimation = (AnimationDrawable)loginLoading.getBackground();
			        loadingAnimation.start();
			        mViewSwitcher.showNext();
			        SinaWeiboHelper.login(AppStart.this, access.getAccessToken(), access.getLoginType());
					
				} else {
					
			        loadingAnimation = (AnimationDrawable)loginLoading.getBackground();
			        loadingAnimation.start();
			        mViewSwitcher.showNext();
					SinaWeiboHelper.authorize(AppStart.this, shareMessage);
				
				}
			}
			
			
			if( v == mLoginNormal) {
				
			}			
		}
	};
    
    /**
     * 跳转到...
     */
	/***
    private void redirectTo(){        
        Intent intent = new Intent(this, Main.class);
        startActivity(intent);
        finish();
    }
    ***/
	
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if(keyCode == KeyEvent.KEYCODE_BACK) {
    		this.onDestroy();
    	}
    	return super.onKeyDown(keyCode, event);
    }
}
